package util;

import java.io.PrintStream;

public class ResultsRegister {
	
	private PrintStream output;
	private long startTime;
	private long endTime;
	private Throwable thrown;
	private long[] results;
	
	public ResultsRegister(PrintStream output) {
		this.output = output;
		this.thrown = null;
	}
	
	public void markStart() {
		startTime = System.currentTimeMillis();
	}
	
	public void markEnd() {
		endTime = System.currentTimeMillis();
	}
	
	public void markError(Throwable t) {
		thrown = t;
	}
	
	public long getExecutionTime() {
		return endTime - startTime;
	}
	
	public void logResults() {
		output.format("start time: %d\n", startTime);
		
		if (thrown != null) {
			output.format("error: %s\n", thrown.getMessage());
		}
		
		output.format("end time: %d\n", endTime);
		output.format("execution time: %d\n", endTime - startTime);
	}

	public void markResults(long[] executionTimes) {
		this.results = executionTimes;
	}
	
	public double getAverage() {
		double sum = 0;
		
		for (long result : results) {
			sum += result;
		}
		
		return sum/results.length;
	}
	
	public double getStandardDeviation() {
		double variance = 0;
		double mean = getAverage();
		
		for (long result : results) {
			variance += (result - mean)*(result - mean);
		}
		
		variance /= results.length;
		return Math.sqrt(variance);
	}
}
